﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.OracleClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Logistics.DataAccess
{
    public class TC_OWI_FILE
    {
        public bool Add(Model.TC_OWI_FILE model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("INSERT INTO TC_OWI_FILE(");
            strSql.Append("TC_OWI001, TC_OWI002,TC_OWI007,TC_OWI008,TC_OWI009)");
            strSql.Append(" VALUES (");
            strSql.Append("@in_TC_OWI001,@in_TC_OWI002,@in_TC_OWI007,@in_TC_OWI008,@in_TC_OWI009)");
            OracleParameter[] cmdParms = {
                DBHelper.CreateDbParameter("@in_TC_OWI001", DbType.String, 10 ,model.TC_OWI001.ToString()),
                DBHelper.CreateDbParameter("@in_TC_OWI002", DbType.String,20, DBValue.GetDBValue(model.TC_OWI002)),
                DBHelper.CreateDbParameter("@in_TC_OWI007", DbType.String,10, DBValue.GetDBValue(model.TC_OWI007)),
                DBHelper.CreateDbParameter("@in_TC_OWI008", DbType.String,10, DBValue.GetDBValue(model.TC_OWI008)),
                DBHelper.CreateDbParameter("@in_TC_OWI009", DbType.String,10, DBValue.GetDBValue(model.TC_OWI009)),
            };

            return DBHelper.ExecuteSql(strSql.ToString(), cmdParms) > 0;
        }

        public bool Update(Model.TC_OWI_FILE model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE TC_OWI_FILE SET ");
            strSql.Append("TC_OWI002=@in_TC_OWI002,");
            strSql.Append("TC_OWI007=@in_TC_OWI007,");
            strSql.Append("TC_OWI008=@in_TC_OWI008,");
            strSql.Append("TC_OWI009=@in_TC_OWI009");
            strSql.Append(" WHERE TC_OWI001=@in_TC_OWI001");
            OracleParameter[] cmdParms = {
                DBHelper.CreateDbParameter("@in_TC_OWI001", DbType.String, 10 ,DBValue.GetDBValue(model.TC_OWI001)),
                DBHelper.CreateDbParameter("@in_TC_OWI002", DbType.String,20, DBValue.GetDBValue(model.TC_OWI002)),
                DBHelper.CreateDbParameter("@in_TC_OWI007", DbType.String,10, DBValue.GetDBValue(model.TC_OWI007)),
                DBHelper.CreateDbParameter("@in_TC_OWI008", DbType.String,10, DBValue.GetDBValue(model.TC_OWI008)),
                DBHelper.CreateDbParameter("@in_TC_OWI009", DbType.String,10, DBValue.GetDBValue(model.TC_OWI009)),
            };

            return DBHelper.ExecuteSql(strSql.ToString(), cmdParms) > 0;
        }

        public Model.TC_OWI_FILE GetModel(string tc_OWI001)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM TC_OWI_FILE ");
            strSql.Append(" WHERE TC_OWI001=@in_TC_OWI001");
            OracleParameter[] cmdParms = {
                DBHelper.CreateDbParameter("@in_TC_OWI001", DbType.String,10 ,DBValue.GetDBValue(tc_OWI001))};

            using (DbDataReader dr = DBHelper.ExecuteReader(strSql.ToString(), cmdParms))
            {
                if (dr.Read())
                {
                    return GetModel(dr);
                }
                return null;
            }
        }

        public bool Delete(string tc_OWI001)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE FROM TC_OWI_FILE ");
            strSql.Append(" WHERE TC_OWI001=@in_TC_OWI001");
            OracleParameter[] cmdParms = {
                DBHelper.CreateDbParameter("@in_TC_OWI001", DbType.String,10 ,DBValue.GetDBValue(tc_OWI001))};

            return DBHelper.ExecuteSql(strSql.ToString(), cmdParms) > 0;
        }

        public List<Model.TC_OWI_FILE> GetList()
        {
            StringBuilder strSql = new StringBuilder("SELECT * FROM TC_OWI_FILE");
            using (DbDataReader dr = DBHelper.ExecuteReader(strSql.ToString()))
            {
                List<Model.TC_OWI_FILE> lst = GetList(dr);
                return lst;
            }
        }

        public List<Model.TC_OWI_FILE> User_GetList(string p_user)
        {
            string l_sql = "SELECT TC_OWI_FILE.* " +
                           "  FROM TC_USERS, TC_PERMISSIONS, TC_OWI_FILE " +
                           " WHERE TC_PERMISSIONS.USERID = TC_USERS.ID " +
                             " AND TC_PERMISSIONS.NAME = TC_OWI_FILE.TC_OWI001 " +
                             " AND TC_PERMISSIONS.TYPE = '2' " +
                             " AND TC_PERMISSIONS.ISGRANTED = '1' " +
                             " AND TC_USERS.USERNAME = '" + p_user + "'";
            StringBuilder strSql = new StringBuilder(l_sql);
            using (DbDataReader dr = DBHelper.ExecuteReader(strSql.ToString()))
            {
                List<Model.TC_OWI_FILE> lst = GetList(dr);
                return lst;
            }
        }

        #region 
        /// <summary>
        /// 由一行数据得到一个实体
        /// </summary>
        private Model.TC_OWI_FILE GetModel(DbDataReader dr)
        {
            Model.TC_OWI_FILE model = new Model.TC_OWI_FILE();
            model.TC_OWI001 = DBHelper.GetString(dr["TC_OWI001"]);
            model.TC_OWI002 = DBHelper.GetString(dr["TC_OWI002"]);
            model.TC_OWI007 = DBHelper.GetString(dr["TC_OWI007"]);
            model.TC_OWI008 = DBHelper.GetString(dr["TC_OWI008"]);
            model.TC_OWI009 = DBHelper.GetString(dr["TC_OWI009"]);
            return model;
        }

        /// <summary>
        /// 由DbDataReader得到泛型数据列表
        /// </summary>
        private List<Model.TC_OWI_FILE> GetList(DbDataReader dr)
        {
            List<Model.TC_OWI_FILE> lst = new List<Model.TC_OWI_FILE>();
            while (dr.Read())
            {
                lst.Add(GetModel(dr));
            }
            return lst;
        }
        #endregion
    }
}
